


//单选

//清空表单
function fomrReset(id){
    document.getElementById(id).reset();
}
/*给单选框或复选框添加样式*/
function checkbox($this){
    if($this.attr('type')=="checkbox"){
        if ($this.attr('checked')=="checked") {
            $this.removeAttr("checked");
            $this.next('img').attr("src","/static/images/c_checkbox_off.png");
        }else{
            $this.attr("checked","checked");
            $this.next('img').attr("src","/static/images/c_checkbox_on.png");
        }
    }
}
var allId = []
var permissionAll = [];
$(function(){
    //user全选
    $('.selectAll-user').click(function(){
        var $list_input=$(this).parents('.tbhead').next('.tbbody-user').find('input[type=checkbox]'); //找到子类的按钮
        var idAll =[];
        if ($(this).attr("checked")==undefined || $(this).attr("checked")=="") {
            $list_input.attr("checked","checked");
            $list_input.next('img').attr("src","/static/images/c_checkbox_on.png");
            $(".tbbody-user input[type='checkbox']").each(function() {
                var id = $(this).attr('data-id')
                idAll.push(id);
                allId=idAll
            })
            console.log(allId)
        }else{
            $list_input.removeAttr("checked");
            $list_input.next('img').attr("src","/static/images/c_checkbox_off.png");
            allId=[];
            console.log(allId)
        }
        checkbox($(this));
    })
//role全选
    $('.SelectAllImg').click(function(){
        var $list_input=$(this).parents('.tbhead').next('.tbbody-role').find('input[type=checkbox]'); //找到子类的按钮
        var idAll =[];
        if ($(this).attr("checked")==undefined || $(this).attr("checked")=="") {
            $list_input.attr("checked","checked");
            $list_input.next('img').attr("src","/static/images/c_checkbox_on.png");
            $(".tbbody-role input[type='checkbox']").each(function() {
                var id = $(this).attr('data-id')
                idAll.push(id);
                allId=idAll
            })
            console.log(allId)
        }else{
            $list_input.removeAttr("checked");
            $list_input.next('img').attr("src","/static/images/c_checkbox_off.png");
            allId=[];
            console.log(allId)
        }
        checkbox($(this));
    })

    //user点击子按钮事件
    //单选
    $("body").on("change",".tbbody-user input[type='checkbox']",function(){

        var $ul_input=$(this).parents('.tbbody-user').prev('.tbhead').find('input');
        if($(this).parents('.tbhead').find("input[checked='checked']").length== $('.checkbox_child').length){
            $ul_input.prop("checked","checked");
            $ul_input.next('img').attr("src","/static/images/c_checkbox_on.png");
        }else{
            $ul_input.prop("checked",false);
            $ul_input.next('img').attr("src","/static/images/c_checkbox_off.png");
        }

        if($(this).attr("checked")=="" || $(this).attr("checked")==undefined){
            $(this).attr("checked",true);
            $(this).next('img').attr("src","/static/images/c_checkbox_on.png");
            var idAll =[];
            $(".tbbody-user input[type='checkbox']:checked").each(function(){
                            var id = $(this).attr("data-id");
                             console.log('正选')
                             idAll.push(id);
                             allId=idAll
                 	})
            console.log(allId)
            if(allId.length==1){
                var userid  = allId['0']
                console.log(userid)
                $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
                $.ajax({
                    type: "POST",
                    url: "/auth/user_manager/user_info",
                    dataType:'json',
                    data: {userid:userid},
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                    },
                    success: function(data){
                        console.log(data)
                        var role_id = data.role_id;
                        var username = data.username

                        $("#username").attr('value',username)
                        $("#rolems").multipleSelect("setSelects", role_id);
                        $("#rolems").multipleSelect("getSelects", "text")
                    }
                });
            }



        }else{
            $(this).attr("checked",false);
            $(this).next('img').attr("src","/static/images/c_checkbox_off.png");
                var id = $(this).attr("data-id");
               var index =  allId.indexOf(id);
                console.log(index)
                if(index>-1){
                    allId.splice(index, 1);
                }else{
                    allId=[]
                }
            console.log(allId)
            if(allId.length==1) {
                var userid  = allId['0']
                console.log(userid)
                $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
                $.ajax({
                    type: "POST",
                    url: "/auth/user_manager/user_info",
                    dataType:'json',
                    data: {userid:userid},
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                    },
                    success: function(data){
                        console.log(data)
                        var role_id = data.role_id;
                        var username = data.username

                        $("#username").attr('value',username)
                        $("#rolems").multipleSelect("setSelects", role_id);
                        $("#rolems").multipleSelect("getSelects", "text")
                    }
                });
            }
        }
    })

    $("body").on("change",".tbbody-role input[type='checkbox']",function(){
        var $ul_input=$(this).parents('.tbbody-role').prev('.tbhead').find('input');
        if($(this).parents('.tbhead').find("input[checked='checked']").length== $('.checkbox_child').length){
            $ul_input.prop("checked","checked");
            $ul_input.next('img').attr("src","/static/images/c_checkbox_on.png");
        }else{
            $ul_input.prop("checked",false);
            $ul_input.next('img').attr("src","/static/images/c_checkbox_off.png");
        }
        if($(this).attr("checked")=="" || $(this).attr("checked")==undefined){
            $(this).attr("checked",true);
            $(this).next('img').attr("src","/static/images/c_checkbox_on.png");

            var idAll =[];
            $(".tbbody-role input[type='checkbox']:checked").each(function(){
                var id = $(this).attr("data-id");
                console.log('正选')
                idAll.push(id);
                allId=idAll
            })

                    console.log(allId)
            if(allId.length==1) {
                var userid  = allId['0']
                console.log(userid)
                $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
                $.ajax({
                    type: "POST",
                    url: "/auth/user_manager/role_info",
                    dataType:'json',
                    data: {userid:userid},
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                    },
                    success: function(data){
                        console.log(data)

                        var role_name = data.role_name
                        var role_description = data.role_description
                        $("#role_name").attr('value',role_name)
                        $("#role_description").attr('value',role_description)


                    }
                });
            }


        }else{
            $(this).attr("checked",false);
            $(this).next('img').attr("src","/static/images/c_checkbox_off.png");
            var id = $(this).attr("data-id");
            var index =  allId.indexOf(id);
            console.log(index)
            if(index>-1){
                allId.splice(index, 1);
            }else{
                allId=[]
            }
            console.log(allId)
            if(allId.length==1){
                var userid  = allId['0']
                console.log(userid)
                $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
                $.ajax({
                    type: "POST",
                    url: "/auth/user_manager/role_info",
                    dataType:'json',
                    data: {userid:userid},
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                    },
                    success: function(data){
                        console.log(data)

                        var role_name = data.role_name
                        var role_description = data.role_description
                        $("#role_name").attr('value',role_name)
                        $("#role_description").attr('value',role_description)


                    }
                });
            }


        }
    })

})
var roleId
//权限管理

$(function(){
    $("body").on('click',"#j-role li",function(){

        var role_id = $(this).attr('data-id')
        var tab_id = 'tab_son_'+role_id
        console.log(tab_id)
        console.log(role_id)
        roleId = role_id
        $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
        $.ajax({
            type: "POST",
            url: "/auth/user_manager/user_manager",
            dataType:'json',
            data: {roleid:roleId},
            headers: {
                'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
            },
            success: function(data){

                permission = data['0']
                pemisionAll = data['1']

                for (var i =0;i<permission.length;i++){
                    for (var j =0;j<pemisionAll.length;j++){
                        if(permission[i]==pemisionAll[j]){

                            $("#"+tab_id).find(".Select_Competence input[type='checkbox']").each(function(){
                                var pemid =  $(this).attr('data-id')

                                if(permission[i]==pemid){

                                         $(this).attr('checked','checked');
                                        $(this).prev('img').attr("src","/static/images/c_checkbox_on.png");

                                }
                            })

                        }else{

                        }
                    }
                }

            }

        });

    })
    })


$(function(){
    var userInput=[];
    var userInputName=[];
    /*新增用户弹窗*/
    $("#sampadd_user").click(function(){
        $('#rolems').change(function() {
            // console.log($(this).val());
            var checkAll =  $(this).val()
            var checkAllName = $(this).text()
            userInput=checkAll;
            userInputName = checkAllName
        }).multipleSelect({

        });
        $('.c-detailress').attr('value','')
        layer.open({
            type:1,
            area: ['500px', ''], //宽高
            title:"新增用户",
            content:$('#j-addUser'),
            btn:["确定","取消"],
            yes:function(){
                //获取多选下拉的val值
                var username = $(".c-detailress").val();
                $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
                $.ajax({
                    type: "POST",
                    url: "/auth/user_manager/user_add",
                    dataType:'json',
                    data: {name:username,check:userInput,inputname:userInputName},
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                    },
                    success: function(data){
                        if(data.status==0){

                            layer.msg(data.msg,{icon:2,time:1500});
                        }else {
                            layer.msg(data.msg,{icon:1,time:2500});
                            setTimeout(function(){
                                location.href='/auth/user_manager/user_manager';
                            },1500)

                        }

                    }
                });
            }
        })
    });

    //新增角色弹窗
    $("#sample_add_role").click(function(){
        layer.open({
            type:1,
            area: ['500px', ''], //宽高
            title:"新增角色",
            content:$('#j-addRole'),
            btn:["确定","取消"],
            yes:function(){
                $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
                $.ajax({
                    type: "POST",
                    url: "/auth/user_manager/role_add",
                    dataType:'json',
                    data: $('#form_addrole').serialize(),
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                    },
                    success: function(data){
                        if(data.status==0){

                            layer.msg(data.msg,{icon:2,time:1500});
                        }else {
                            layer.msg(data.msg,{icon:1,time:2500});
                            setTimeout(function(){
                                location.href='/auth/user_manager/user_manager';
                            },1500)

                        }

                    }
                });

            }
        })
    })
    /*编辑用户弹窗*/
    $("#sample_edit_2").click(function(){
        if(allId.length==0){
            layer.msg('请选择您要编辑的用户',{icon:2,time:1500});
            return;
        }else if(allId.length>1){
            layer.msg('一次只能编辑一个用户',{icon:2,time:1500});
            return;
        }else{
        var userid  = allId['0']
        console.log(userid)
        $('#rolems').change(function() {
            // console.log($(this).val());
            //分别是role_id  和名陈
            var checkAll =  $(this).val()
            var checkAllName = $(this).text()
            userInput=checkAll;
            userInputName = checkAllName

        }).multipleSelect({

        });

        layer.open({
            type:1,
            area: ['500px', ''], //宽高
            title:"编辑用户",
            content:$('#j-addUser'),
            btn:["确定","取消"],
            yes:function(){
                console.log(userid)
                //获取多选下拉的val值
                var username = $("#username").val();
                $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
                $.ajax({
                    type: "POST",
                    url: "/auth/user_manager/user_update",
                    dataType:'json',
                    data: {userid:userid,name:username,check:userInput},
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                    },
                    success: function(data){
                        if(data.status==0){

                            layer.msg(data.msg,{icon:2,time:1500});
                        }else {
                            layer.msg(data.msg,{icon:1,time:2500});
                            setTimeout(function(){
                                location.href='/auth/user_manager/user_manager';
                            },1500)

                        }

                    }
                });
            }
        })
        }
    });
//删除用户
    $('.j-delete').click(function(){

        if(allId.length==0){
            layer.msg('请选择您要删除的用户',{icon:2,time:1500});
            return;
        }
        layer.confirm('您确定要删除？', {
            btn: ['是的','再想想'] //按钮
        }, function(){
            $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
            $.ajax({
                type: "POST",
                url: "/auth/user_manager/user_delete",
                dataType:'json',
                data: {name:allId},
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                },
                success: function(data){
                    if(data.status==0){

                        layer.msg(data.msg,{icon:2,time:2500});

                    }else {
                        layer.msg(data.msg,{icon:1,time:1500});
                        setTimeout(function(){
                            location.href='/auth/user_manager/user_manager';
                        },1000)

                    }

                }
            });
        }, function(){
            layer.msg('下次要想清楚哦', {
                time: 20000, //20s后自动关闭
                btn: ['明白了', '知道了']
            });
        });


    })
    //删除角色
    $('#role_delete_3').click(function(){
        if(allId.length==0){
            layer.msg('请选择您要删除的角色',{icon:2,time:1500});
            return;
        }
        layer.confirm('您确定要删除？', {
            btn: ['是的','再想想'] //按钮
        }, function(){
            $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
            $.ajax({
                type: "POST",
                url: "/auth/user_manager/role_delete",
                dataType:'json',
                data: {name:allId},
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                },
                success: function(data){
                    if(data.status==0){

                        layer.msg(data.msg,{icon:2,time:2500});

                    }else {
                        layer.msg(data.msg,{icon:1,time:1500});
                        setTimeout(function(){
                            location.href='/auth/user_manager/user_manager';
                        },1000)

                    }

                }
            });
        }, function(){
            layer.msg('下次要想清楚哦', {
                time: 20000, //20s后自动关闭
                btn: ['明白了', '知道了']
            });
        });

    })
    //编辑角色
    $('#role_edit_2').click(function(){
        if(allId.length==0){
            layer.msg('请选择您要编辑的角色',{icon:2,time:1500});
            return;
        }else if(allId.length>1){
            layer.msg('一次只能编辑一个角色',{icon:2,time:1500});
            return;
        }else{
            var userid  = allId['0']
            console.log(userid)

            layer.open({
                type:1,
                area: ['500px', ''], //宽高
                title:"编辑角色",
                content:$('#j-addRole'),
                btn:["确定","取消"],
                yes:function(){
                    console.log(userid)
                    //获取多选下拉的val值
                    var name = $("#role_name").val();
                    var description = $("#role_description").val();
                    console.log(name)
                    console.log(description)
                    $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
                    $.ajax({
                        type: "POST",
                        url: "/auth/user_manager/role_update",
                        dataType:'json',
                        data: {userid:userid,name:name,description:description},
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                        },
                        success: function(data){
                            if(data.status==0){

                                layer.msg(data.msg,{icon:2,time:1500});
                            }else {
                                layer.msg(data.msg,{icon:1,time:2500});
                                setTimeout(function(){
                                    location.href='/auth/user_manager/user_manager';
                                },1500)

                            }

                        }
                    });
                }
            })
        }
        })
    //重至密码
    $('#sample_reset_4').click(function(){

        if(allId.length==0){
            layer.msg('请选择您要密码重置的用户',{icon:2,time:1500});
            return;
        }
        layer.confirm('您确定要密码重置？', {
            btn: ['是的','再想想'] //按钮
        }, function(){
            $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
            $.ajax({
                type: "POST",
                url: "/auth/user_manager/password_reset",
                dataType:'json',
                data: {name:allId},
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                },
                success: function(data){
                    if(data.status==0){

                        layer.msg(data.msg,{icon:2,time:2500});

                    }else {
                        layer.msg(data.msg,{icon:1,time:1500});
                        setTimeout(function(){
                            location.href='/auth/user_manager/user_manager';
                        },1000)

                    }

                }
            });
        }, function(){
            layer.msg('下次要想清楚哦', {
                time: 20000, //20s后自动关闭
                btn: ['明白了', '知道了']
            });
        });
    })




    //用户角色单独的--切换后之前的checkbox为空
    $("#singletabs li").on('click',function(){
        console.info($("#singletabs li").not(this).text());
        var picTabNum = $(this).index();
        allId = []
        permissionAll = [];
        var firstId = $(this).attr('data-id')
        if(firstId!=''){
            roleId = firstId
        }else{
            roleId = ['']
        }


        var tab_id = 'tab_son_'+firstId
        //获取默认角色信息
        $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
        $.ajax({
            type: "POST",
            url: "/auth/user_manager/user_manager",
            dataType:'json',
            data: {roleid:roleId},
            headers: {
                'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
            },
            success: function(data){
                permission = data['0']
                pemisionAll = data['1']
                console.log(permission)
                console.log(pemisionAll)

                for (var i =0;i<permission.length;i++){
                    for (var j =0;j<pemisionAll.length;j++){
                        if(permission[i]==pemisionAll[j]){

                            console.log("#"+tab_id)
                            $("#"+tab_id).find(".Select_Competence input[type='checkbox']").each(function(){
                               var pemid =  $(this).attr('data-id')
                                if(permission[i]==pemid){
                                   console.info(permission[i])

                                        $(this).attr('checked','checked');
                                       $(this).prev('img').attr("src","/static/images/c_checkbox_on.png");

                                }
                            })

                        }else{

                        }
                    }
                }



            }
        });
        var lis_checkbox = $(".tab-pane.onlytab").eq(picTabNum).siblings().find('input[type=checkbox]');
        if (lis_checkbox.attr("checked")=="checked" || lis_checkbox.attr("checked")==undefined) {
            lis_checkbox.attr("checked",false);
            lis_checkbox.next('img').attr("src","/static/images/c_checkbox_off.png");
        }
    })



// 权限分配-2018-7-27
    //全选


    $('.allselect').click(function(){
        //如果是off没有选中的图片才给她增加on 并执行便利
        var $list_input=$(this).parents('.all-btner').prev().find('input[type=checkbox]');
        var $thisVal = $(this).find('input[type=checkbox]');
        if ($list_input.prev('img').prop("src","/static/images/c_checkbox_off.png")) {
            $thisVal.prop("checked","checked");
            var chk_value =[]; //定义一个数组
            $thisVal.parents('.all-btner').prev().find('input[type=checkbox]').each(function() {
                $list_input.prop("checked","checked");
                $list_input.prev('img').prop("src","/static/images/c_checkbox_on.png");
                chk_value.push($(this).attr('data-id'));//将选中的值添加到数组chk_value中

            });
            console.info("一项所选项：" + chk_value )
            var tab_id = 'tab_son_'+roleId
            var permission = []
            $("#"+tab_id).find(".permission-dl input[type='checkbox']:checked").each(function() {
                var id = $(this).attr('data-id')
                permission.push(id);
                permissionAll=permission;
            })
            console.log(permissionAll)

            $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
            $.ajax({
                type: "POST",
                url: "/auth/user_manager/role_permissions",
                dataType:'json',
                data: {roleid:roleId,name:permissionAll},
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                },
                success: function(data){
                    if(data.status==0){

                        layer.msg(data.msg,{icon:2,time:2500});

                    }else {
                        layer.msg(data.msg,{icon:1,time:1500});
                        //

                    }

                }
            });

        }else{
        }
    });
    //全不选-重置
    $('.revselect').click(function(){
        var chk_value =[]; //定义一个数组
        var strSel = '';
        var $list_input=$(this).parents('.all-btner').prev().find('input[type=checkbox]');
        var $thisVal = $(this).find('input[type=checkbox]');
        if ($list_input.prev('img').prop("src","/static/images/c_checkbox_on.png")) {
            $thisVal.prop("checked","");
            $list_input.prev('img').prop("src","/static/images/c_checkbox_off.png");
            $list_input.prop("checked","");
            permissionAll = chk_value
            console.info("所有反选项："+permissionAll);
            var permission = []
            var tab_id = 'tab_son_'+roleId
            $("#"+tab_id).find(".permission-dl input[type='checkbox']:checked").each(function() {
                var id = $(this).attr('data-id')
                console.log(id)
                permission.push(id);
                permissionAll=permission;
            })
            if(permission.length==0){
                permission = ['']
                permissionAll=permission
            }
            console.log(permissionAll)
            $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
            $.ajax({
                type: "POST",
                url: "/auth/user_manager/role_permissions",
                dataType:'json',
                data: {roleid:roleId,name:permissionAll},
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                },
                success: function(data){
                    if(data.status==0){

                        layer.msg(data.msg,{icon:2,time:2500});

                    }else {
                        layer.msg(data.msg,{icon:1,time:1500});
                        //

                    }

                }
            });

        }else{

        }
    })





    $(".permission-dl input[type='checkbox']").change(function(){
        if($(this).prop("checked")=="checked"){
            $(this).prop("checked",false);
            $(this).prev('img').prop("src","/static/images/c_checkbox_off.png");
            var permission = []
            var tab_id = 'tab_son_'+roleId
            $("#"+tab_id).find(".permission-dl input[type='checkbox']:checked").each(function() {
                var id = $(this).attr('data-id')
                permission.push(id);
                console.log('取消选择')
                permissionAll=permission
            })
            if(permission.length==0){
                permission = ['']
                permissionAll=permission
            }
            console.log(permissionAll)

            $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
            $.ajax({
                type: "POST",
                url: "/auth/user_manager/role_permissions",
                dataType:'json',
                data: {roleid:roleId,name:permissionAll},
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                },
                success: function(data){
                    if(data.status==0){

                        layer.msg(data.msg,{icon:2,time:2500});

                    }else {
                        layer.msg(data.msg,{icon:1,time:1500});
                        //

                    }

                }
            });

        }else{
            $(this).prop("checked",true);
            $(this).prev('img').prop("src","/static/images/c_checkbox_on.png");
            var permission = []
            var tab_id = 'tab_son_'+roleId
            $("#"+tab_id).find(".permission-dl input[type='checkbox']:checked").each(function() {
                var id = $(this).attr('data-id')
                console.log(id)
                permission.push(id);
                console.log('选择')
                permissionAll=permission
            })
            console.log(permissionAll)
            console.log(roleId)
            $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});

            $.ajax({
                type: "POST",
                url: "/auth/user_manager/role_permissions",
                dataType:'json',
                data: {roleid:roleId,name:permissionAll},
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                },
                success: function(data){
                    if(data.status==0){

                        layer.msg(data.msg,{icon:2,time:2500});

                    }else {
                        layer.msg(data.msg,{icon:1,time:1500});
                        //

                    }

                }
            });
        }
    })
})
//关闭弹窗-layer
function closedHandler(){
    var index = parent.layer.getFrameIndex(window.name);
    //刷新父层
    parent.location.reload();
    //关闭弹出层
    parent.layer.close(index);
}
//选择客户下拉显示
function changesel(obj,className){
    var val = obj.value;

    if(val){
        $('#cust_id').val(2);
    }else{
        $('#cust_id').val('');
    }
    var datatrr = $(obj).find("option:selected").attr("data-val");
    var radioattr = $(obj).find("input[name='billtype']:checked").attr("data-val");
    if(className == "" || className == undefined){
        //0，2，3，17
        if(datatrr != "0" && datatrr != "2" && datatrr != "3" && datatrr != "17"){
            $("#deliveryChange").slideDown()
        }else{
            $("#deliveryChange").slideUp()
        }

    }else if(className == "fapiao"){
        if(radioattr != "不开发票"){
            layer.open({
                title: '信息提示',
                content:"开发票操作将在提交后发起OA告知财务",
            });
        }
    }else{
        $("#"+className+val).slideDown().siblings().slideUp()
    }
}

//帅选事件
$(function() {
    //用户名输入
    $(".searchUser input").bind('keypress',function (event) {
        if(event.keyCode == "13")

        {
            var user_name = $(this).val();
            $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
            $.ajax({
                type: "POST",
                url: "/auth/user_manager/user_screen",
                dataType: 'json',
                data: {user_name: user_name},
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                },
                success: function (data) {

                    if(data.status==1){
                        var type=''
                        var type_class = ''
                        layer.msg(data.msg,{icon:1,time:1500});
                        var userinfo = data.userinfo.data
                        console.log(userinfo.length)
                        console.log(userinfo)
                        var tr = $(".screen").empty();
                        for (var i = 0; i < userinfo.length; i++) {
                            var str = "";
                            if(userinfo[i].status==1){
                                type='启用'
                                type_class='c-put orderedt'
                            }else{
                                type='禁止'
                                type_class='c-put orderdel'
                            }
                            str = '<tr><td><label><input data-id="'+userinfo[i].id+'" type="checkbox" class="checkson checkbox_child"/><img src="/static/images/c_checkbox_off.png"/></label></td><td>'+i+'</td><td class="name">'+userinfo[i].username+'</td><td class="role_name">'+userinfo[i].role_name+'</td><td>'+userinfo[i].username+'</td><td>'+userinfo[i].created_at+'</td><td>'+userinfo[i].updated_at+'</td><td class="lasttd"><a class="'+type_class+'" href="javascript:;">'+type+'</a></td></tr>'
                            tr.append(str);
                            console.log(str)
                        }


                        console.log(userinfo)

                    }else{
                        layer.msg(data.msg,{icon:2,time:1500});
                    }

                }

            });
        }


    })
    //下拉框
    $("#search_role").change(function(){
       var role_name =$(this).find('option:selected').attr('value');
       console.log(role_name)
        $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
        $.ajax({
            type: "POST",
            url: "/auth/user_manager/role_screen",
            dataType: 'json',
            data: {role_name: role_name},
            headers: {
                'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
            },
            success: function (data) {

                if(data.status==1){
                    var type=''
                    var type_class = ''
                    layer.msg(data.msg,{icon:1,time:1500});
                    var userinfo = data.userinfo.data
                    console.log(userinfo.length)
                    console.log(userinfo)
                    var tr = $(".screen").empty();
                    for (var i = 0; i < userinfo.length; i++) {
                        var str = "";
                        if(userinfo[i].status==1){
                            type='启用'
                            type_class='c-put orderedt'
                        }else{
                            type='禁止'
                            type_class='c-put orderdel'
                        }

                        str = '<tr><td><label><input data-id="'+userinfo[i].id+'" type="checkbox" class="checkson checkbox_child"/><img src="/static/images/c_checkbox_off.png"/></label></td><td>'+i+'</td><td class="name">'+userinfo[i].username+'</td><td class="role_name">'+userinfo[i].role_name+'</td><td>'+userinfo[i].username+'</td><td>'+userinfo[i].created_at+'</td><td>'+userinfo[i].updated_at+'</td><td class="lasttd"><a class="'+type_class+'" href="javascript:;">'+type+'</a></td></tr>'
                        tr.append(str);
                        console.log(str)

                    }



                }else{
                    layer.msg(data.msg,{icon:2,time:1500});
                }

            }

        });

    })

//    时间帅选
    $(".time_screen input").change(function () {
       var beginTime =  $("#beginTime").val()
        var endTime =  $("#endTime").val()
        console.log(beginTime)
        console.log(endTime)
        $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
        $.ajax({
            type: "POST",
            url: "/auth/user_manager/time_screen",
            dataType: 'json',
            data: {beginTime: beginTime,endTime:endTime},
            headers: {
                'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
            },
            success: function (data) {

                if(data.status==1){
                    var type=''
                    var type_class = ''
                    layer.msg(data.msg,{icon:1,time:1500});
                    var userinfo = data.userinfo.data
                    var tr = $(".screen").empty();
                    for (var i = 0; i < userinfo.length; i++) {
                        var str = "";


                        if(userinfo[i].status==1){
                            type='启用'
                            type_class='c-put orderedt'
                        }else{
                            type='禁止'
                            type_class='c-put orderdel'
                        }

                        str = '<tr><td><label><input data-id="'+userinfo[i].id+'" type="checkbox" class="checkson checkbox_child"/><img src="/static/images/c_checkbox_off.png"/></label></td><td>'+i+'</td><td class="name">'+userinfo[i].username+'</td><td class="role_name">'+userinfo[i].role_name+'</td><td>'+userinfo[i].username+'</td><td>'+userinfo[i].created_at+'</td><td>'+userinfo[i].updated_at+'</td><td class="lasttd"><a class="'+type_class+'" href="javascript:;">'+type+'</a></td></tr>'

                        tr.append(str);


                    }
                    console.log(userinfo)

                }else{
                    layer.msg(data.msg,{icon:2,time:1500});
                }

            }

        });

    })

    $(".login_time_screen input").change(function(){

        var loginLatyTime = $("#loginLatyTime").val()
        var loginOverTime = $("#loginOverTime").val()
        $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
        $.ajax({
            type: "POST",
            url: "/auth/user_manager/login_time_screen",
            dataType: 'json',
            data: {loginLatyTime: loginLatyTime,loginOverTime:loginOverTime},
            headers: {
                'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
            },
            success: function (data) {

                if(data.status==1){
                    var type=''
                    var type_class = ''
                    layer.msg(data.msg,{icon:1,time:1500});
                    var userinfo = data.userinfo.data
                    var tr = $(".screen").empty();
                    for (var i = 0; i < userinfo.length; i++) {
                        var str = "";


                        if(userinfo[i].status==1){
                            type='启用'
                            type_class='c-put orderedt'
                        }else{
                            type='禁止'
                            type_class='c-put orderdel'
                        }

                        str = '<tr><td><label><input data-id="'+userinfo[i].id+'" type="checkbox" class="checkson checkbox_child"/><img src="/static/images/c_checkbox_off.png"/></label></td><td>'+i+'</td><td class="name">'+userinfo[i].username+'</td><td class="role_name">'+userinfo[i].role_name+'</td><td>'+userinfo[i].username+'</td><td>'+userinfo[i].created_at+'</td><td>'+userinfo[i].updated_at+'</td><td class="lasttd"><a class="'+type_class+'" href="javascript:;">'+type+'</a></td></tr>'

                        tr.append(str);


                    }
                    console.log(userinfo)

                }else{
                    layer.msg(data.msg,{icon:2,time:1500});
                }

            }

        });
    })

//    修改状态
    $("#type_screen").change(function(){

        var status =$(this).find('option:selected').attr('value');
        console.log(status)
        $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
        $.ajax({
            type: "POST",
            url: "/auth/user_manager/user_type_screen",
            dataType: 'json',
            data: {status:status},
            headers: {
                'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
            },
            success: function (data) {
                console.log(data)
                if(data.status==1){
                    var type=''
                    var type_class = ''
                    layer.msg(data.msg,{icon:1,time:1500});
                    var userinfo = data.userinfo.data
                    var tr = $(".screen").empty();
                    for (var i = 0; i < userinfo.length; i++) {
                        var str = "";


                        if(userinfo[i].status==1){
                            type='启用'
                            type_class='c-put orderedt'
                        }else{
                            type='禁止'
                            type_class='c-put orderdel'
                        }

                        str = '<tr><td><label><input data-id="'+userinfo[i].id+'" type="checkbox" class="checkson checkbox_child"/><img src="/static/images/c_checkbox_off.png"/></label></td><td>'+i+'</td><td class="name">'+userinfo[i].username+'</td><td class="role_name">'+userinfo[i].role_name+'</td><td>'+userinfo[i].username+'</td><td>'+userinfo[i].created_at+'</td><td>'+userinfo[i].updated_at+'</td><td class="lasttd"><a class="'+type_class+'" href="javascript:;">'+type+'</a></td></tr>'

                        tr.append(str);


                    }
                    console.log(userinfo)

                }else{
                    layer.msg(data.msg,{icon:2,time:1500});
                }

            }

        });

    })

//    点击修改status
    $(".update-status a").click(function(){

    var id = $(this).parents("tr").find("input").attr('data-id')
    var status = $(this).attr('data-status')

    $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
    $.ajax({
        type: "POST",
        url: "/auth/user_manager/update_user_status",
        dataType: 'json',
        data: {id:id,status:status},
        headers: {
            'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
        },
        success: function (data) {

            if(data.status==1){

                layer.msg(data.msg,{icon:1,time:1500});
                setTimeout(function(){
                    location.href='/auth/user_manager/user_manager';
                },500)


            }else{
                layer.msg(data.msg,{icon:2,time:1500});
            }

        }

    });

})

//角色管理赛选
    $("#role_manager_screen").change(function(){
        var role_id =$(this).find('option:selected').attr('data-id');

        console.log(role_id)
        $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
        $.ajax({
            type: "POST",
            url: "/auth/user_manager/role_manager_screen",
            dataType: 'json',
            data: {role_id:role_id},
            headers: {
                'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
            },
            success: function (data) {

                if(data.status==1){
                    var type=''
                    var type_class = ''
                    layer.msg(data.msg,{icon:1,time:1500});
                    var userinfo = data.userinfo.data
                    var tr = $(".tbbody").empty();
                    for (var i = 0; i < userinfo.length; i++) {
                        var str = "";


                        if(userinfo[i].status==1){
                            type='启用'
                            type_class='c-put orderedt'
                        }else{
                            type='禁止'
                            type_class='c-put orderdel'
                        }

                        str = '<tr><td><label><input data-id="'+userinfo[i].id+'" type="checkbox" class="checkson checkbox_child"/><img src="/static/images/c_checkbox_off.png"/> </label></td> <td>'+i+'</td><td>'+userinfo[i].name+'</td> <td>'+userinfo[i].description+'</td> <td>'+userinfo[i].created_time+' - '+userinfo[i].updated_time+'</td> <td class="lasttd"><a class="'+type_class+'" href="javascript:;">'+type+'</a></td></tr>'

                        tr.append(str);


                    }
                    console.log(userinfo)

                }else{
                    layer.msg(data.msg,{icon:2,time:1500});
                }

            }

        });
    })

    $(".serch_role_description input").bind('keypress',function (event) {
        if(event.keyCode == "13")

        {
            var role_description = $(this).val();
            $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
            $.ajax({
                type: "POST",
                url: "/auth/user_manager/serch_role_description",
                dataType: 'json',
                data: {role_description:role_description},
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                },
                success: function (data) {

                    if(data.status==1){
                        var type=''
                        var type_class = ''
                        layer.msg(data.msg,{icon:1,time:1500});
                        var userinfo = data.userinfo.data
                        var tr = $(".tbbody").empty();
                        for (var i = 0; i < userinfo.length; i++) {
                            var str = "";


                            if(userinfo[i].status==1){
                                type='启用'
                                type_class='c-put orderedt'
                            }else{
                                type='禁止'
                                type_class='c-put orderdel'
                            }

                            str = '<tr><td><label><input data-id="'+userinfo[i].id+'" type="checkbox" class="checkson checkbox_child"/><img src="/static/images/c_checkbox_off.png"/> </label></td> <td>'+i+'</td><td>'+userinfo[i].name+'</td> <td>'+userinfo[i].description+'</td> <td>'+userinfo[i].created_time+' - '+userinfo[i].updated_time+'</td> <td class="lasttd"><a class="'+type_class+'" href="javascript:;">'+type+'</a></td></tr>'

                            tr.append(str);


                        }
                        console.log(userinfo)

                    }else{
                        layer.msg(data.msg,{icon:2,time:1500});
                    }

                }

            });

        }


    })

    $(".role_time input").change(function () {
        var role_beginTime =  $("#role_beginTime").val()
        var role_endTime =  $("#role_endTime").val()
        console.log(role_beginTime)
        console.log(role_endTime)
        $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
        $.ajax({
            type: "POST",
            url: "/auth/user_manager/role_time_screen",
            dataType: 'json',
            data: {role_beginTime: role_beginTime,role_endTime:role_endTime},
            headers: {
                'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
            },
            success: function (data) {

                if(data.status==1){
                    var type=''
                    var type_class = ''
                    layer.msg(data.msg,{icon:1,time:1500});
                    var userinfo = data.userinfo.data
                    var tr = $(".tbbody").empty();
                    for (var i = 0; i < userinfo.length; i++) {
                        var str = "";


                        if(userinfo[i].status==1){
                            type='启用'
                            type_class='c-put orderedt'
                        }else{
                            type='禁止'
                            type_class='c-put orderdel'
                        }

                        str = '<tr><td><label><input data-id="'+userinfo[i].id+'" type="checkbox" class="checkson checkbox_child"/><img src="/static/images/c_checkbox_off.png"/> </label></td> <td>'+i+'</td><td>'+userinfo[i].name+'</td> <td>'+userinfo[i].description+'</td> <td>'+userinfo[i].created_time+' - '+userinfo[i].updated_time+'</td> <td class="lasttd"><a class="'+type_class+'" href="javascript:;">'+type+'</a></td></tr>'

                        tr.append(str);


                    }
                    console.log(userinfo)

                }else{
                    layer.msg(data.msg,{icon:2,time:1500});
                }

            }

        });

    })

    $("#role_status_screen").change(function(){
        var status =$(this).find('option:selected').attr('value');
        console.log(status)
        $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
        $.ajax({
            type: "POST",
            url: "/auth/user_manager/role_status_screen",
            dataType: 'json',
            data: {status: status},
            headers: {
                'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
            },
            success: function (data) {
            console.log(data)
                if(data.status==1){
                    var type=''
                    var type_class = ''
                    layer.msg(data.msg,{icon:1,time:1500});
                    var userinfo = data.userinfo.data
                    var tr = $(".tbbody").empty();
                    for (var i = 0; i < userinfo.length; i++) {
                        var str = "";


                        if(userinfo[i].status==1){
                            type='启用'
                            type_class='c-put orderedt'
                        }else{
                            type='禁止'
                            type_class='c-put orderdel'
                        }

                        str = '<tr><td><label><input data-id="'+userinfo[i].id+'" type="checkbox" class="checkson checkbox_child"/><img src="/static/images/c_checkbox_off.png"/> </label></td> <td>'+i+'</td><td>'+userinfo[i].name+'</td> <td>'+userinfo[i].description+'</td> <td>'+userinfo[i].created_time+' - '+userinfo[i].updated_time+'</td> <td class="lasttd"><a class="'+type_class+'" href="javascript:;">'+type+'</a></td></tr>'

                        tr.append(str);


                    }
                    console.log(userinfo)

                }else{
                    layer.msg(data.msg,{icon:2,time:1500});
                }

            }

        });

    })

    $(".update_role_status a").click(function(){

        var id = $(this).parents("tr").find("input").attr('data-id')
        console.log(id)
        var status = $(this).attr('data-status')

        $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
        $.ajax({
            type: "POST",
            url: "/auth/user_manager/update_role_status",
            dataType: 'json',
            data: {id:id,status:status},
            headers: {
                'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
            },
            success: function (data) {

                if(data.status==1){

                    layer.msg(data.msg,{icon:1,time:1500});
                    setTimeout(function(){
                        location.href='/auth/user_manager/user_manager';
                    },500)


                }else{
                    layer.msg(data.msg,{icon:2,time:1500});
                }

            }

        });
    })

})






